<template>
  <div class="box">
    <header class="header detail">
      <transition name='fade'>
        <van-nav-bar v-if="top<50"
          @click-left="$router.back()"
          @click-right.stop="rightNav"
          >
          <template #left>
            <van-icon name="arrow-left" size="24" />
          </template>
          <template #right>
            <van-icon name="weapp-nav" size="24" />
            <ul class="rightNavList" v-show="show">
              <li>首页</li>
              <li>分类</li>
              <li>购物车</li>
              <li>我的</li>
              <li @click="showShare = true">分享</li>
            </ul>
          </template>
        </van-nav-bar>
      </transition>
      <van-share-sheet
        v-model="showShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
      />
      <transition name="fade">
        <van-nav-bar class="scrollNav"
          v-if="top>=50"
          @click-left="$router.back()"
          @click-right.stop="rightNav"
          >
          <template #left>
            <van-icon name="arrow-left" size="24" />
          </template>
          <template #title>
            <p :class="top<=200 ? 'active' : ''">信息</p>
            <p :class="top>200&&top<500 ? 'active' : ''">评价</p>
            <p :class="top>=500&&top<800 ? 'active' : ''">详情</p>
            <p :class="top>=800 ? 'active' : ''">推荐</p>
          </template>
          <template #right>
            <van-icon name="weapp-nav" size="24" />
          </template>
        </van-nav-bar>
      </transition>
    </header>
    <div class="proCont" ref="content">
      <!-- <p>{{$route.params.id}}</p> -->
      <!-- <p>{{id}}</p> -->
      <van-swipe class="my-swipe detail" indicator-color="white">
        <van-swipe-item v-for="(item,idx) in proImg" :key="idx" @click="proview(idx)">
          <van-image width="100%" height="4rem" fit="cover" :src="item" />
        </van-swipe-item>
      </van-swipe>
      <!-- <p>接收到的id为：{{$route.params.id}}</p> -->
      <div class="proInfo">
        <h3>商品名称：{{proName}}</h3>
        <p>品牌：{{brand}}</p>
        <p>商品描述：{{introduce}}</p>
        <p>分类：{{column}}</p>
        <p>价格：{{price}}</p>
        <p>销量：{{sales}}</p>
        <p>库存：{{stock}}</p>
        <p>折扣：{{discount}}</p>
        <p>评分：{{score}}</p>
      </div>
      <div class="proInfo">
        <h3>商品名称：{{proName}}</h3>
        <p>品牌：{{brand}}</p>
        <p>商品描述：{{introduce}}</p>
        <p>分类：{{column}}</p>
        <p>价格：{{price}}</p>
        <p>销量：{{sales}}</p>
        <p>库存：{{stock}}</p>
        <p>折扣：{{discount}}</p>
        <p>评分：{{score}}</p>
      </div>
      <div class="proInfo">
        <h3>商品名称：{{proName}}</h3>
        <p>品牌：{{brand}}</p>
        <p>商品描述：{{introduce}}</p>
        <p>分类：{{column}}</p>
        <p>价格：{{price}}</p>
        <p>销量：{{sales}}</p>
        <p>库存：{{stock}}</p>
        <p>折扣：{{discount}}</p>
        <p>评分：{{score}}</p>
      </div>
      <div class="proInfo">
        <h3>商品名称：{{proName}}</h3>
        <p>品牌：{{brand}}</p>
        <p>商品描述：{{introduce}}</p>
        <p>分类：{{column}}</p>
        <p>价格：{{price}}</p>
        <p>销量：{{sales}}</p>
        <p>库存：{{stock}}</p>
        <p>折扣：{{discount}}</p>
        <p>评分：{{score}}</p>
      </div>
    </div>
    <van-goods-action>
      <van-goods-action-icon icon="chat-o" text="客服" color="#07c160" />
      <van-goods-action-icon icon="cart-o" text="购物车" :badge="sum" @click="$router.push('/cart')" />
      <van-goods-action-icon :icon="isStar ? 'star' : 'star-o'" @click="star" :text="isStar ? '已收藏' : '收藏'" color="#ff5000" />
      <van-goods-action-button type="warning" text="加入购物车" @click="addCart" />
      <van-goods-action-button type="danger" text="立即购买" />
    </van-goods-action>
  </div>
</template>

<script>
// pro?id=1f31jg31l3
// 发送↓
// /pro/1f31jg31l3
// 定义↓
// /pro/:id

import Vue from 'vue'
import { Swipe, SwipeItem, Image as VanImage, ImagePreview, GoodsAction, GoodsActionIcon, GoodsActionButton, NavBar, Icon, ShareSheet, Toast } from 'vant'

import { getDetail, addCart } from '../../api/index.js'

Vue.use(Swipe)
Vue.use(SwipeItem)
Vue.use(VanImage)
Vue.use(GoodsAction)
Vue.use(GoodsActionIcon)
Vue.use(GoodsActionButton)
Vue.use(NavBar)
Vue.use(Icon)
Vue.use(ShareSheet)
Vue.use(Toast)

export default {
  data () {
    return {
      brand: '',
      column: '',
      discount: 10,
      introduce: '',
      logo: '',
      price: 0,
      proImg: '',
      proName: '',
      sales: 0,
      score: 5,
      stock: 0,
      top: 0,
      show: false,
      showShare: false,
      options: [
        { name: '微信', icon: 'wechat' },
        { name: '微博', icon: 'weibo' },
        { name: '复制链接', icon: 'link' },
        { name: '分享海报', icon: 'poster' },
        { name: '二维码', icon: 'qrcode' },
        { name: '简书', icon: 'qrcode' }
      ],
      isStar: false,
      sum: 0
    }
  },
  props: ['id'],
  mounted () {
    console.log(this.$store)
    this.sum = this.$store.getters.sum
    // console.log(this.$route.params.id)
    // 根据路由跳转时携带的商品id,请求数据库中的商品详情数据
    getDetail({
      // id: this.$route.params.id
      id: this.id
    }).then(res => {
      // 解析数据
      this.proImg = [res.data.data.proImg, 'https://img.yzcdn.cn/vant/apple-1.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg']
      this.proName = res.data.data.proName
      this.brand = res.data.data.brand
      this.discount = res.data.data.discount
      this.column = res.data.data.column
      this.introduce = res.data.data.introduce
      this.price = res.data.data.price
      this.sales = res.data.data.sales
      this.stock = res.data.data.stock
      this.score = res.data.data.score
    })
    this.$refs.content.addEventListener('scroll', () => {
      this.top = this.$refs.content.scrollTop
      console.log(this.top)
    })
    document.addEventListener('click', () => {
      this.show = false
    })
    let starData = localStorage.getItem('star')
    starData = starData ? JSON.parse(starData) : []
    const onoff = starData.some((val, idx) => {
      return val === this.id
    })
    if (onoff) {
      this.isStar = true
    }
  },
  methods: {
    proview (id) {
      ImagePreview({
        images: this.proImg,
        startPosition: id
      })
    },
    addCart () {
      console.log(this.$store)
      if (this.$store.state.user.isLogin !== 'ok') {
        addCart({
          userId: localStorage.getItem('userId'),
          proId: this.id
        }).then(res => {
          console.log(res.data)
        })
      } else {
        this.$router.push('/login')
      }
    },
    rightNav () {
      this.show = !this.show
    },
    onSelect (option) {
      Toast(option.name)
      this.showShare = false
    },
    star () {
      this.isStar = !this.isStar
      // [id,id,id,id,.....]
      let starData = localStorage.getItem('star')
      starData = starData ? JSON.parse(starData) : []
      let i = 0
      const onoff = starData.some((val, idx) => {
        i = idx
        return val === this.id
      })
      if (onoff) {
        starData.splice(i, 1)
      } else {
        starData.push(this.id)
      }
      localStorage.setItem('star', JSON.stringify(starData))
    }
  }
}
</script>

<style lang="scss">
.header.detail{
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9;
  width: 100%;
  height: 0.46rem;
  text-align: center;
  font: 0.24rem/0.46rem "";
  .van-nav-bar{
    background-color: rgba(0,0,0,0);
    position: absolute;
    left:0;top:0;
    width: 100%;
    &::after{
      border:none;
    }
    .van-nav-bar__right{
      background: #333;
      padding: 12px;
      border-radius:50%;
    }
    .van-nav-bar__left{
      background: #333;
      padding: 12px;
      border-radius:50%;
    }
    .van-icon{
      color: #fff;
    }
  }
  .scrollNav.van-nav-bar{
    background-color: #fff;
    .van-nav-bar__left{
      background: none;
    }
    .van-nav-bar__right{
      background: none;
    }
    .van-icon{
      color: #333;
    }
    .van-nav-bar__title{
      width: 100%;
      display: flex;
      justify-content: space-around;
      p{
        margin: 0;
        padding: 4px 0;
        flex: 1;
        &.active{
          border-bottom: 4px solid red;
        }
      }
    }
  }
  .rightNavList{
    position: absolute;
    width:1rem;
    top: 0.5rem;
    right: 0;
    background: #222;
    color: #fff;
    list-style: none;
    margin: 0;
    padding: 0 0.1rem;
    li{
      margin: 0.1rem 0;
      text-align: left;
      border-bottom: solid 1px #efefef;
    }
  }
}
.box{
  display: flex;
  .proCont{
    flex:1;
    overflow: auto;
    .proInfo{
      padding: 0 0.1rem 0.5rem;
    }
  }
}
.my-swipe.detail{
  height:4rem;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 1s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>
